﻿using System;
using System.Windows.Forms;
using DevExpress.XtraTabbedMdi;
using Moonlite.Data.Configuration;
using Moonlite.ExtensionApi.GlobalInterfaces;
using Moonlite.Platform.General;
using Moonlite.Platform.Project;

namespace Moonlite.UI.Shell.Controls
{
    public partial class StartPage : Form
    {
        private bool UpdateSelection { get; set; }
        public static bool ClosePageOnProjectLoad { get; set; }
        public static bool ShowPageOnStartup { get; set; }
        public IProject SelectedProject { get; private set; }
        public XtraMdiTabPage ParentTabPage { get; internal set; }
        public void LoadPageSettings()
        {
            checkClosePageAfterProjectLoad.Checked = ClosePageOnProjectLoad;
            checkShowPageOnStartup.Checked = ShowPageOnStartup;
            LoadRecentProjects();
        }

        public void LoadRecentProjects()
        {
            RecentProjects.Projects = RecentProjectsSerializer.LoadRecentProjects(Application.LocalUserAppDataPath + "\\Settings\\RecentProjects.xml");
            RecentProjects.Projects = RecentProjects.Projects.RemoveDuplicates();

            int currentSelectedIndex = listBoxControlRecentProjects.SelectedIndex;
            IProject currentSelectedProject = SelectedProject;
            UpdateSelection = false;
            listBoxControlRecentProjects.Items.Clear();
            UpdateSelection = true;
            foreach (Project project in RecentProjects.Projects)
            {
                listBoxControlRecentProjects.Items.Add(new StartPageRecentProject(project.Name, project));
            }

            Global.Instance.UpdateRecentProjects();
            if (currentSelectedProject == null)
            {
                return;
            }

            bool containsProject = false;
            foreach (Project project in RecentProjects.Projects)
            {
                if (project.Name != currentSelectedProject.Name || project.Path != currentSelectedProject.Path)
                {
                    continue;
                }

                containsProject = true;
                break;
            }

            if (containsProject == false)
            {
                return;
            }

            SelectedProject = currentSelectedProject;
            listBoxControlRecentProjects.SelectedIndex = currentSelectedIndex;
        }

        public void SavePageSettings()
        {
            SaveRecentProjects();
            LoadRecentProjects();
        }

        public static void SaveRecentProjects()
        {
            RecentProjectsSerializer.SaveRecentProjects(RecentProjects.Projects);
        }

        private void OnRecentProjectsListBoxSelectedIndexChanged(object sender, SelectedIndexChangedEventArgs e)
        {
            SavePageSettings();
            if (ClosePageOnProjectLoad == true)
            {
                SavePageSettings();
                Dispose();
            }

            if (listBoxControlRecentProjects.Items.Count > listBoxControlRecentProjects.SelectedIndex)
            {
                SelectedProject = (listBoxControlRecentProjects.Items[listBoxControlRecentProjects.SelectedIndex] as StartPageRecentProject).Tag as Project;
                Global.Instance.OpenProject(SelectedProject);
            }
        }

        private void OnNewProjectClick(object sender, EventArgs e)
        {
            Global.Instance.CreateProject();
        }

        private void OnOpenProjectClick(object sender, EventArgs e)
        {
            Global.Instance.LaunchOpenProjectDialog();
        }

        private void OnClosePageAfterProjectLoadCheckedChanged(object sender, EventArgs e)
        {
            ClosePageOnProjectLoad = checkClosePageAfterProjectLoad.Checked;
            SavePageSettings();
            ConfigurationManager.UpdateProperty(Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("General"), "CloseStartPageOnProjectLoad");
        }

        private void OnShowPageOnStartupCheckedChanged(object sender, EventArgs e)
        {
            ShowPageOnStartup = checkShowPageOnStartup.Checked;
            SavePageSettings();
            ConfigurationManager.UpdateProperty(Moonlite.Shell.Attachment.ConfigurationInstance.GetElementByName("General"), "ShowStartPageOnStartup");
        }

        private void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            SavePageSettings();
        }

        public StartPage()
        {
            InitializeComponent();
        }
    }
}
